package com.fengmi.order.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.fengmi.order.dto.OrderDTO;
import com.fengmi.order.entity.OmsOrder;
import com.fengmi.order.entity.OmsOrderItem;
import com.fengmi.order.service.IOmsOrderItemService;
import com.fengmi.order.service.IOmsOrderService;
import com.fengmi.utils.id.IdWorker;
import com.fengmi.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 订单表 前端控制器
 * </p>
 *
 * @author zhuxm
 * @since 2022-03-14
 */
@RestController
@RequestMapping("/order")
public class OmsOrderController {

    @Autowired
    private IOmsOrderService omsOrderService;

    @Autowired
    private IOmsOrderItemService omsOrderItemService;

    @RequestMapping("save")
    public ResultVO save(@RequestBody OrderDTO orderDTO){

        return  omsOrderService.save(orderDTO);

    }

    @Autowired
    private IdWorker idWorker;


    @RequestMapping("genId")
    public String genId(){

        return  idWorker.nextId()+"L";
    }



    @RequestMapping("info/{ordersn}")
    public OmsOrder findOrderInfoByOrdersn(@PathVariable String ordersn){

        QueryWrapper<OmsOrder> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_sn",ordersn);

        OmsOrder omsOrder = omsOrderService.getOne(queryWrapper);



        QueryWrapper<OmsOrderItem> omsOrderItemQueryWrapper = new QueryWrapper<>();
        omsOrderItemQueryWrapper.eq("order_sn",ordersn);

        List<OmsOrderItem> itemList = omsOrderItemService.list(omsOrderItemQueryWrapper);


        omsOrder.setItemList(itemList);


        return omsOrder;
    }



    @RequestMapping("update/{ordersn}")
    public ResultVO update(@PathVariable String ordersn){

        UpdateWrapper<OmsOrder> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("order_sn",ordersn)
                .set("status",1);

        boolean update = omsOrderService.update(updateWrapper);


        return new ResultVO(update,update?"success":"fail");
    }
}
